<?php
/**
 * Created by PhpStorm.
 * User: Rei
 * Date: 16/4/30
 * Time: 22:16
 * Mail: rei@king.moe
 */
namespace Admin\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function index()
    {
        $this->display('/login');
    }

    //验证登陆表单
    public function check()
    {
        $username = check_str(I('username'));
        $password = I('password', '', 'md5');
        $user = M('admin')->where(array('username' => $username))->find();
        if (!$user || strcmp($user['password'], $password) !== 0) {
            $this->error('用户名或者密码错误');
            exit;
        }

        session('id', $user['id']);
        session('admin', $user['username']);

        $this->redirect('/Admin/Index');
    }

    //登出
    public function logout()
    {
        //销毁session
        session('[destroy]');
        $this->redirect('/Admin/Login');
    }
}